package com.langsin.blog.dao.impl;

import com.langsin.blog.dao.UserDao;
import com.langsin.blog.domain.Roles;
import com.langsin.blog.domain.User;
import com.langsin.blog.util.HibernateUtils;
import org.hibernate.Session;
import org.junit.Test;

import java.util.List;

public class UserDaoImp implements UserDao{

    private Session session;
    private String hql;

    @Test
    public void test(){
        System.out.println(this.userSelectByAccount("root"));
    }

    public User userSelectByAccount(String account) {
        User user = null;
        hql = "FROM User user WHERE user.account = ?";
        session = HibernateUtils.getSession();
        try {
            List<User> list = session.createQuery(hql).setParameter(0,account).list();
            if(list.isEmpty()){
                return null;
            }else{
                user = list.get(0);
                return user;
            }
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally {
            HibernateUtils.closeSession(session);
        }
    }

    public User userSelectById(int id) {
        User user = null;
        session = HibernateUtils.getSession();
        try{
            user = session.get(User.class,id);
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            HibernateUtils.closeSession(session);
        }
        return user;
    }

    public List<User> userSelectByNickname(String nickname) {
        List<User> list = null;
        hql = "FROM User user WHERE user.nickname = ?";
        session = HibernateUtils.getSession();
        try {
            list = session.createQuery(hql).setParameter(0,nickname).list();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            HibernateUtils.closeSession(session);
        }
        return list;
    }

    public boolean userAdd(User user) {
        session = HibernateUtils.getSession();
        try {
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
            return true;
        }catch(Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }finally {
           HibernateUtils.closeSession(session);
        }
    }

    public boolean userModifyRoles(int id, String prop, Roles roles){
        session = HibernateUtils.getSession();
        try {
            session.beginTransaction();
            User user = session.load(User.class,id);
            user.setRoles(roles);
            session.getTransaction().commit();return true;
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }finally{
            HibernateUtils.closeSession(session);
        }
    }

    public boolean userModify(int id, String prop, String value) {
        session = HibernateUtils.getSession();
        try {
            session.beginTransaction();
            User user = session.load(User.class,id);
            if("nickname".equals(prop)){
                user.setNickname(value);
            }else if("password".equals(prop)){
                user.setPassword(value);
            }else{
                System.err.println("无此属性");
            }
            session.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }finally {
            HibernateUtils.closeSession(session);
        }
    }
}
